﻿@if (Request.IsAuthenticated)
{
    @Html.Partial("~/Views/Popup/Index.cshtml")
    var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
@*Get userid*@
    Guid? user_id;
    if (uid != "")
    {
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    else
    {
        KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
        string un = User.Identity.Name;
        bcontrol.ressUserID(un);
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
@*Get userid*@
    <script type="text/javascript">
        var dicIcon = {
            "tblLichSuKH": "font-icon-user",
            "tblLichSuLoaiThe": "font-icon-bookmark-empty",
            "tblLichSuLuatTinhDiem": "font-icon-foursquare",
            "tblLichSuLuatCapThe": "font-icon-foursquare",
            "tblLichSuTheVip": "font-icon-bookmark",
            "tblLichSuTour": "font-icon-briefcase",
            "tblLichSuTourKH": "font-icon-fighter-jet"
        };
        var notification = function () {
            $.ajax({
                type: "GET",
                cache: false,
                url: "@Url.Action("UpdateNotify", "Notify", new { user_id = @user_id })" + "&time=" + Math.random(),
                success: function (msg) {
                    var minTime = $('.notify_content li.notify').first().attr('id');
                    var tId = parseInt(minTime.replace("notify_", ""));
                    if (msg) {
                        for (i = 0; i < msg.length; i++) {
                            var id = 'notify_' + msg[i]["notify_id"];
                            if ($('#' + id).length <= 0) {
                                if (msg[i]["notify_id"] && parseInt(msg[i]["notify_id"]) > tId) {
                                    var color;
                                    var url = "@Url.Action("PopupDetail", "Notify")" + "?malichsu=" + msg[i]["maLichSu"] + "&tablename=" + msg[i]["table_name"];
                                    var cl;
                                    if (msg[i]["loaiSuaDoi"] == -1)
                                    { color = "#FA5833"; cl = "warning" }
                                    else if (msg[i]["loaiSuaDoi"] == 0)
                                    { color = "#F4A506"; cl = "update" }
                                    else { color = "#5AAD34"; cl = "addnew" }

                                    $('' +
                                    '<li class="' + cl + '" id="notify_' + msg[i]["notify_id"] + '">'
                                        + '<a id="notify-click"  href="' + url + '">'
                                            + '<i class="' + dicIcon[msg[i]["table_name"]] + '" style="color:' + color + '"></i>'
                                            + '<span class="message"> '
                                                + msg[i]["noiDung"].substr(0, 25)
                                            + '</span>'
                                            + '<span class="time" cur="@(KimPhongCRM.Lib.libTimer.ToJsTimeTicks(DateTime.Now))"' +
                                            ' rel="' + msg[i]["ngaySuaTicks"] + '">1 min</span>'
                                        + '</a>'
                                    + '</li>').hide().prependTo('.dropdown-menu.notifications .notify_content').slideDown();
                                    $('span.time', '#' + id).each(function (i, item) {
                                        var rel = $(item).attr('rel');
                                        rel = parseInt(rel);
                                        var cur = parseInt($(item).attr('cur'));
                                        var diff = cur - rel;
                                        var newdate = new Date(rel);

                                        var format = 'MS';
                                        var layout = '{mn} {ml}, {sn} {sl}';
                                        var format = 'S';
                                        var layout = '{sn} {sl}';

                                        if (diff >= 86400000) {
                                            format = 'D';
                                            layout = '{dn} {dl}';
                                        } else if (diff >= 3600000) {
                                            format = 'H';
                                            layout = '{hn} {hl}';
                                        } else if (diff >= 60000) {
                                            format = 'M';
                                            layout = '{mn} {ml}';
                                        }

                                        $(item).countdown({ since: newdate, format: format, layout: layout });
                                    });

                                    $('.dropdown-menu.notifications .notify_content li:nth-child(n+11)').slideUp().remove();

                                    $.gritter.add({
                                        // (string | mandatory) the heading of the notification
                                        title: 'Thông báo từ hệ thống',
                                        // (string | mandatory) the text inside the notification
                                        text: msg[i]["noiDung"] + '<a style="color:#78CD51" id="notify-click"  href="' + url + '"> Xem chi tiết</a>',
                                        // (string | optional) the image to display on the left
                                        image: '',
                                        // (bool | optional) if you want it to fade out on its own or just sit there
                                        sticky: false,
                                        // (int | optional) the time you want it to be alive for before fading out
                                        time: '10000'
                                    });
                                }

                            }
                            //alert(msg[0]["nguoiSua"])
                        }

                    }
                    else {
                        //alert(msg);
                    }
                },
                error: function (msg) {
                    //alert(msg);
                }
            });
        };
        $(document).ready(function () {
            setInterval(notification, 15000);
        });
    </script>
    
    <div class="nav-no-collapse header-nav">
        <ul class="nav pull-right">
            <!-- start: Notifications Dropdown -->
            @{Html.RenderAction("GetTop10", "Notify", new { user_id = @user_id });}
            <!-- start: Customer Birthday -->
            @{Html.RenderAction("GetBirthday7", "Notify");}
            <!-- start: Message Dropdown -->
            @*{Html.RenderAction("GetMessageTop10", "Notify");}*@
            <!-- end: Message Dropdown -->
            <li>
                <a class="btn" href="#">
                    <i class="icon-wrench icon-white"></i>
                </a>
            </li>
            <!-- start: User Dropdown -->
            <li class="dropdown">
                <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                    <i class="icon-user icon-white"></i>
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="@Url.Action("Profile", "Account")"><i class="icon-user"></i>Profile</a></li>
                    <li><a href="@Url.Action("LogOff", "Account")"><i class="icon-off"></i>Logout</a></li>
                </ul>
            </li>
            <!-- end: User Dropdown -->
        </ul>
    </div>
}
else
{
    <div class="nav-no-collapse header-nav">
        <ul class="nav pull-right">
            <li class="dropdown">
                <ul class="dropdown-menu">
                    <li><a href="@Url.Action("Login", "Account")"><i class="icon-user"></i>Login</a></li>
                    <li><a href="@Url.Action("Register", "Account")"><i class="fa-icon-key"></i>Register</a></li>
                </ul>
            </li>
        </ul>
    </div>
}